package Model.Dao;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import Model.Pojo.Khachhang;
import Model.Pojo.Parameters;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Administrator
 */
public class DAOProvider
{

    /**
     * Insert du lieu
     *
     * @param session
     * @param obj
     * @throws Exception
     */
    public static void Insert(Session session, Object obj) throws Exception
    {
        Transaction tran = null;

        try
        {
            tran = session.beginTransaction();

            session.save(obj);

            tran.commit();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.getMessage());
        }
    }

    /**
     * Select list
     *
     * @param session
     * @param query
     * @param para
     * @return
     * @throws Exception
     */
    public static List Select(final Session session, final String query, final Parameters[] para)
            throws
            Exception
    {
        List list = null;
        try
        {
            Query result = session.createQuery(query);
            for (int i = 0; i < para.length; i++)
            {
                result.setParameter(para[i].getName(), para[i].getValue());
            }
            list = result.list();
        }
        catch (Exception ex)
        {
            System.out.print(ex.getMessage());
            throw new Exception(ex.getMessage());
        }
        finally
        {
        }

        return list != null ? list : null;
    }

    /**
     * Select one
     *
     * @param session
     * @param objClass
     * @param id
     * @return
     */
    public static Object SelectOne(final Session session, Class objClass, Object id) throws Exception
    {
        Object objValue = null;
        try
        {
            objValue = (Object) session.get(objClass, (Serializable) id);
        }
        catch (Exception ex)
        {
            throw new Exception(ex.getMessage());

        }
        return objValue;
    }

    /**
     * Update du lieu
     *
     * @param session
     * @param obj
     * @throws Exception
     */
    public static void Update(Session session, Object obj) throws Exception
    {
        Transaction tran = null;

        try
        {
            tran = session.beginTransaction();

            session.update(obj);

            tran.commit();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.getMessage());
        }
    }

    /**
     * Xóa khach hang
     *
     * @param session
     * @param obj
     * @throws Exception
     */
    public static void Delete(Session session, Object obj) throws Exception
    {
        Transaction tran = null;

        try
        {
            tran = session.beginTransaction();

            session.delete(obj);

            tran.commit();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.getMessage());
        }
    }
}
